// pages/music/index/index.js
Page({

  InnerAudioContext: null,
  /**
   * 页面的初始数据
   */
  data: {
    musics: [],
    playing: false,
    showInfo:[true,false,false],
  },
  swiperChangeHandler(event) {

    this.InnerAudioContext.pause()
    this.setData({
      playing: false
    })

    // if(event.detail.current === 0){
    //     wx.navigateTo({
    //       url: '/pages/history/history',
    //     });
    // }
  },
  handleIconClick(event) {
    console.log(event);
    const index = event.currentTarget.dataset.index; // 获取被点击图标的索引
    let showInfo = this.data.showInfo.slice(); // 深拷贝当前showInfo数组
    for (let i = 0; i < showInfo.length; i++) {
      if (i === index) {
        showInfo[i] = true; // 切换当前索引的状态
      } else {
        showInfo[i] = false; // 隐藏其他信息
      }
    }
    this.setData({ showInfo }); // 更新数据
    // console.log(showInfo);
  },
  play(event) {
    // console.log(event.currentTarget.dataset.play.music_id);
    if (event.currentTarget.dataset.play.music_id) {
      this.InnerAudioContext.src = event.currentTarget.dataset.play.music_id
    }
    this.InnerAudioContext.play()
    this.setData({
      playing: !this.data.playing
    })
  },

  pause() {
    this.InnerAudioContext.pause()
    this.setData({
      playing: !this.data.playing
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.InnerAudioContext = wx.createInnerAudioContext()

    wx.setNavigationBarTitle({
      title: '音乐推荐',
    });
    wx.request({
      url: 'http://localhost:8000/api/music/',
      success: response => {
        if (response.statusCode === 200) {
          response.data.data.forEach(id => {
            wx.request({
              url: 'http://localhost:8000/api/music/detail/' + id,
              success: detailResponse => {
                if (response.statusCode === 200) {
                  this.setData({
                    musics: this.data.musics.concat(detailResponse.data.data)
                  });
                  console.log(this.data.musics);
                  // console.log(detailResponse.data.data.hp_author);
                }
              }
            })
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.InnerAudioContext.pause();
    this.setData({
      playing: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})